package com.fetch;

import java.util.List;
import com.fetch.bean.Profile;
import com.fetch.common.Configuration;
import com.fetch.fetcher.DetailFetcher;
import com.fetch.fetcher.ListFetcher;
import com.fetch.ui.Status;


public class Crawler {

	private ListFetcher listFetcher = null;
	private DetailFetcher detailFetcher = new DetailFetcher();
	private Status status = null;
	private int category = 0;
	
	public Crawler(int category, Status status){
		this.category = category;
		this.status = status;
	}
	
	public void start(){
		status.reset();
		new Thread(){
			public void run() {
				crawl();
				Main.self.complete();
			};
		}.start();
	}
	
	private void crawl(){
		int pageStart = Configuration.geInt(category + ".page.start");
		int pageEnd = Configuration.geInt(category + ".page.end");
		
		status.setTotalPage(pageEnd-pageStart+1);
		for(int page=pageStart; page<=pageEnd&&Main.self.started; page++){
			try{
				listFetcher = new ListFetcher(category);
				List<Profile> profiles = listFetcher.fetch(page);
				listFetcher.close();
				status.setTotal(profiles.size());
				for(int i=0; i<profiles.size()&&Main.self.started; i++){
//					detailFetcher.currPape = page;
					detailFetcher.fetch(profiles.get(i));
					status.setComplete(i+1);
				}
				status.completePageIncr();
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(null != listFetcher){
					listFetcher.close();
				}
			}
		}
	}
	
//	private void crawl(){
//		Main.self.status.setTotalPage(5788);
//		for(int page=5787; page>=0&&Main.self.started; page--){
//			try{
//				listFetcher = new ListFetcher();
//				List<Company> profiles = listFetcher.fetch(page+1);
//				listFetcher.close();
//				Main.self.status.setTotal(profiles.size());
//				for(int i=0; i<profiles.size(); i++){
//					detailFetcher.fetch(profiles.get(i));
//					Main.self.status.setComplete(i+1);
//				}
//				Main.self.status.setCompletePage(5787-page);
//			}catch (Exception e) {
//				e.printStackTrace();
//			}finally{
//				if(null != listFetcher){
//					listFetcher.close();
//				}
//			}
//		}
//	}
	
	public void stop(){
		if(null != listFetcher){
			listFetcher.close();
		}
		if(null != detailFetcher){
			detailFetcher.close();
		}
	}
	
}
